package com.wuhao.code.check.ui

import com.intellij.openapi.options.Configurable
import com.intellij.openapi.ui.ValidationInfo
import com.intellij.ui.components.JBLabel
import com.intellij.ui.components.JBTextField
import com.intellij.util.ui.FormBuilder
import javax.swing.JComponent
import javax.swing.JPanel

/**
 * 插件配置界面
 * @author 吴昊
 * @since 1.0
 */
class PluginConfigurable : Configurable {

    private var userField: JBTextField? = null
    private var maxLinesPerFileField: JBTextField? = null
    private var maxLinesPerFunctionField: JBTextField? = null
    private var mainPanel: JPanel? = null

    override fun getDisplayName(): String {
        return "Java Code Check"
    }

    override fun createComponent(): JComponent? {
        val settings = PluginSettings.INSTANCE
        
        userField = JBTextField(settings.user)
        maxLinesPerFileField = JBTextField(settings.maxLinesPerFile)
        maxLinesPerFunctionField = JBTextField(settings.maxLinesPerFunction)

        mainPanel = FormBuilder.createFormBuilder()
            .addLabeledComponent(JBLabel("用户名:"), userField!!, 1, false)
            .addLabeledComponent(JBLabel("文件最大行数:"), maxLinesPerFileField!!, 1, false)
            .addLabeledComponent(JBLabel("方法最大行数:"), maxLinesPerFunctionField!!, 1, false)
            .addComponentFillVertically(JPanel(), 0)
            .panel

        return mainPanel
    }

    override fun isModified(): Boolean {
        val settings = PluginSettings.INSTANCE
        return userField?.text != settings.user ||
                maxLinesPerFileField?.text != settings.maxLinesPerFile ||
                maxLinesPerFunctionField?.text != settings.maxLinesPerFunction
    }

    override fun apply() {
        val settings = PluginSettings.INSTANCE
        settings.user = userField?.text ?: ""
        settings.maxLinesPerFile = maxLinesPerFileField?.text ?: "1500"
        settings.maxLinesPerFunction = maxLinesPerFunctionField?.text ?: "100"

        val validation = validateInput()
        if (validation != null) {
            // 可以在这里显示错误信息
        }
    }

    override fun reset() {
        val settings = PluginSettings.INSTANCE
        userField?.text = settings.user
        maxLinesPerFileField?.text = settings.maxLinesPerFile
        maxLinesPerFunctionField?.text = settings.maxLinesPerFunction
    }

    override fun disposeUIResources() {
        userField = null
        maxLinesPerFileField = null
        maxLinesPerFunctionField = null
        mainPanel = null
    }

    /**
     * 验证输入
     */
    private fun validateInput(): ValidationInfo? {
        val fileLines = maxLinesPerFileField?.text
        val functionLines = maxLinesPerFunctionField?.text

        if (fileLines.isNullOrBlank()) {
            return ValidationInfo("文件最大行数不能为空", maxLinesPerFileField)
        }

        if (functionLines.isNullOrBlank()) {
            return ValidationInfo("方法最大行数不能为空", maxLinesPerFunctionField)
        }

        try {
            val fileLinesInt = fileLines.toInt()
            if (fileLinesInt <= 0) {
                return ValidationInfo("文件最大行数必须大于0", maxLinesPerFileField)
            }
        } catch (e: NumberFormatException) {
            return ValidationInfo("文件最大行数必须是有效数字", maxLinesPerFileField)
        }

        try {
            val functionLinesInt = functionLines.toInt()
            if (functionLinesInt <= 0) {
                return ValidationInfo("方法最大行数必须大于0", maxLinesPerFunctionField)
            }
        } catch (e: NumberFormatException) {
            return ValidationInfo("方法最大行数必须是有效数字", maxLinesPerFunctionField)
        }

        return null
    }
} 